home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / python-support / gnome-orca / orca / brlmon.py < prev    next >
Encoding:
Python Source  |  2009-04-13  |  6.6 KB  |  196 lines

  1. # Orca
  2. #
  3. # Copyright 2006-2008 Sun Microsystems Inc.
  4. #
  5. # This library is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU Library General Public
  7. # License as published by the Free Software Foundation; either
  8. # version 2 of the License, or (at your option) any later version.
  9. #
  10. # This library is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. # Library General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Library General Public
  16. # License along with this library; if not, write to the
  17. # Free Software Foundation, Inc., Franklin Street, Fifth Floor,
  18. # Boston MA  02110-1301 USA.
  19.  
  20. """Provides a graphical braille display at the top of the screen.
  21. This is mainly for debugging and for demonstrations."""
  22.  
  23. __id__        = "$Id: brlmon.py 3882 2008-05-07 18:22:10Z richb $"
  24. __version__   = "$Revision: 3882 $"
  25. __date__      = "$Date: 2008-05-07 14:22:10 -0400 (Wed, 07 May 2008) $"
  26. __copyright__ = "Copyright (c) 2005-2008 Sun Microsystems Inc."
  27. __license__   = "LGPL"
  28.  
  29. import gtk
  30.  
  31. # Attribute/Selection mask strings:
  32. #
  33. DOT_7 =   '\x40' # 01000000
  34. DOT_8 =   '\x80' # 10000000
  35. DOTS_78 = '\xc0' # 11000000
  36.     
  37. # Markup strings:
  38. #
  39. NORMAL = " size='xx-large'"
  40.  
  41. CURSOR_CELL_EMPTY =   " background='black'" \
  42.                     + " weight='ultrabold'" \
  43.                     + " style='italic'"
  44.  
  45. CURSOR_CELL =   " background='white'" \
  46.               + " weight='ultrabold'" \
  47.               + " style='italic'" \
  48.  
  49. ATTRIBUTE_7 = " underline='low'"
  50.  
  51. ATTRIBUTE_8 = " underline='error'"
  52.  
  53. ATTRIBUTE_78 = " underline='double'"
  54.  
  55. class BrlMon(gtk.Window):
  56.  
  57.     """Displays a GUI braille monitor that mirrors what is being
  58.     shown on the braille display.  This currently needs a lot of
  59.     work, but it is a start.  TODO's include doing a better job of
  60.     docking it at the top of the display (e.g., make all other
  61.     windows move out from underneath it), doing better highlighting of
  62.     the cursor cell, allowing the font to be set, and perhaps allowing
  63.     clicks to simulate cursor routing keys."""
  64.  
  65.     def __init__(self, numCells=32, cellWidth=25, cellHeight=50):
  66.         """Create a new BrlMon.
  67.  
  68.         Arguments:
  69.         - numCells: how many braille cells to make
  70.         - cellWidth: width of each cell in pixels
  71.         - cellHeight: height of each cell in pixels
  72.         """
  73.  
  74.         gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
  75.         self.set_title("Braille Monitor")
  76.         self.set_default_size(cellWidth * numCells, cellHeight)
  77.         hbox = gtk.HBox(True)
  78.         self.add(hbox)
  79.         self.cellFrames = []
  80.         self.cellLabels = []
  81.         i = 0
  82.         while (i < numCells):
  83.             frame = gtk.Frame()
  84.             frame.set_shadow_type(gtk.SHADOW_OUT)
  85.             label = gtk.Label(" ")
  86.             label.set_use_markup(True)
  87.             frame.add(label)
  88.             hbox.add(frame)
  89.             self.cellFrames.append(frame)
  90.             self.cellLabels.append(label)
  91.             i += 1
  92.  
  93.         # This prevents it from getting focus.
  94.         #
  95.         self.set_property("accept-focus", False)
  96.         self.connect_after("check-resize", self.onResize)
  97.  
  98.     def onResize(self, obj):
  99.         """Tell the window to be a dock and set its struts, which I
  100.         thinks means to attempt to glue it somewhere on the display.
  101.         """
  102.  
  103.         # We know what we are doing here, so tell pylint not to flag
  104.         # the self.window method calls as errors (e.g., Class 'window' 
  105.         # has no 'get_size' member).  The disable-msg is localized to
  106.         # just this method.
  107.         #
  108.         # pylint: disable-msg=E1101
  109.  
  110.         screen_width = gtk.gdk.screen_width()
  111.         [window_width, window_height] = self.window.get_size()
  112.         if window_width < screen_width:
  113.             x = (screen_width - window_width) / 2
  114.         else:
  115.             x = 0
  116.  
  117.         self.window.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DOCK)
  118.         self.window.property_change(
  119.                 gtk.gdk.atom_intern("_NET_WM_STRUT_PARTIAL", False),
  120.                 gtk.gdk.atom_intern("CARDINAL", False), 32,
  121.                 gtk.gdk.PROP_MODE_REPLACE,
  122.                 [0,                     # LEFT
  123.                  0,                     # RIGHT
  124.                  window_height,         # TOP
  125.                  0,                     # BOTTOM
  126.                  0,                     # LEFT_START
  127.                  0,                     # LEFT_END
  128.                  0,                     # RIGHT_START
  129.                  0,                     # RIGHT_END
  130.                  0,                     # TOP_START
  131.                  screen_width - 1,      # TOP_END
  132.                  0,                     # BOTTOM_START
  133.                  0])                    # BOTTOM_END
  134.  
  135.         self.move(x, 0)
  136.  
  137.     def writeText(self, cursorCell, string, mask=None):
  138.         """Display the given text and highlight the given
  139.         cursor cell.  A cursorCell of 0 means no cell has
  140.         the cursor.
  141.  
  142.         Arguments:
  143.         - cursorCell: 1-based index of cell with cursor
  144.         - string: len must be <= num cells.
  145.         """
  146.  
  147.         # Fill out the cells from the string.
  148.         #
  149.         try:
  150.             string = string.decode("UTF-8")
  151.         except:
  152.             string = ""
  153.  
  154.         for i in range(0, len(string)):
  155.  
  156.             # Handle special chars so they are not interpreted by pango.
  157.             #
  158.             if string[i] == "<":
  159.                 char = "<"
  160.             elif string[i] == "&":
  161.                 char = "&"
  162.             elif string[i] == "\t":
  163.                 char = "$t"
  164.             else:
  165.                 char = string[i]
  166.  
  167.             markup = NORMAL
  168.             if i == (cursorCell - 1):
  169.                 if string[i] == " ":
  170.                     markup += CURSOR_CELL_EMPTY
  171.                 else:
  172.                     markup += CURSOR_CELL
  173.                 self.cellFrames[i].set_shadow_type(
  174.                     gtk.SHADOW_IN)
  175.             else:
  176.                 self.cellFrames[i].set_shadow_type(
  177.                     gtk.SHADOW_OUT)
  178.  
  179.             if mask:
  180.                 if (mask[i] == DOTS_78):
  181.                     markup += ATTRIBUTE_78
  182.                 elif (mask[i] == DOT_7):
  183.                     markup += ATTRIBUTE_7
  184.                 elif (mask[i] == DOT_8):
  185.                     markup += ATTRIBUTE_8
  186.  
  187.             self.cellLabels[i].set_markup(
  188.                 "<span" + markup + ">%s</span>" % char)
  189.  
  190.         # Pad the rest
  191.         #
  192.         for i in range(len(string), len(self.cellFrames)):
  193.             self.cellLabels[i].set_text(" ")
  194.             self.cellFrames[i].set_shadow_type(
  195.                 gtk.SHADOW_OUT)
  196.